package timing.ukulele.auth.security;


import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import timing.ukulele.common.util.CryptoUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * 认证鉴权工具
 */
@Slf4j
public class SecurityUtils {

    private static String privateKey;

    private SecurityUtils() {
        // 禁止实例化工具类
        throw new UnsupportedOperationException("Utility classes cannot be instantiated.");
    }

    /**
     * 提取请求中的参数并转为一个map返回
     *
     * @param request 当前请求
     * @return 请求中的参数
     */
    public static MultiValueMap<String, String> getParameters(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(parameterMap.size());
        parameterMap.forEach((key, values) -> {
            for (String value : values) {
                parameters.add(key, value);
            }
        });
        return parameters;
    }

    public static String decryptPassword(String encryptString) {
//        System.out.println("解密前：" + encryptString);
        String pbs = getPrivateKey();
//        System.out.println("私钥:" + pbs);
        try {
            String result = CryptoUtils.decryptByRSA(pbs, encryptString);
//            System.out.println("解密后：" + result);
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String getPrivateKey() {
        if (StringUtils.hasText(privateKey))
            return privateKey;
        InputStream ins = SecurityUtils.class.getClassLoader().getResourceAsStream("privateKey.txt");
        if (ins != null) {
            try {
                BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(ins));
                StringBuilder tStringBuffer = new StringBuilder();
                String sTempOneLine;
                while ((sTempOneLine = tBufferedReader.readLine()) != null) {
                    tStringBuffer.append(sTempOneLine);
                }
                String str = tStringBuffer.toString();
                privateKey = str.replaceAll("[\t\n\r]", "");
            } catch (Exception ex) {
                ex.printStackTrace();
                privateKey = null;
            }
        } else {
            privateKey = null;
        }
        return privateKey;
    }

    public static void main(String[] args) throws Exception {
        String privateKey = "MIICdgIBADANBg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5zM78qDdjVWjK7px1ZQCXbSuroBZtKe1It0QwA0rvJx5ClK0yd2HJZwO/d1I7qXQ31I54xyKBTBuEa8Zlgmd36LWS/EBERBNCKTgZgj+oCO9PRnj+IMdQZ20Hg=";
// 解密后的明文应该为test
        String plainText = CryptoUtils.decryptByRSA(privateKey, cipherText);
        System.out.println("解密后的明文为：" + plainText);
    }

}